//
//  FANEmoticonViewModel.swift
//  Weibo - Swift
//
//  Created by user on 16/4/26.
//  Copyright © 2016年 user. All rights reserved.
//

import UIKit

let FANEmoticonCol:CGFloat = 7

let FANEmoticonRow:CGFloat = 3

let FANEmoticonCount:CGFloat = FANEmoticonCol * FANEmoticonRow - 1


class FANEmoticonViewModel: NSObject {
    
    static let sharedEmoticon:FANEmoticonViewModel = FANEmoticonViewModel()
    
    private lazy var path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("emoticons.archvier")
    
    lazy var emoticonBundle:NSBundle = {
        
       let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        
        return NSBundle(path: path)!
        
    }()
    
    lazy var recentEmoticons:[FANEmoticon] = {
       
        if let recents = NSKeyedUnarchiver.unarchiveObjectWithFile(self.path) as? [FANEmoticon]{
            
            return recents
            
        }else{
            
            return [FANEmoticon]()
        }
        
    }()
    
    func saveRecentEmoticon(emoticon:FANEmoticon){
        
        var emoticons = allEmoticons[0][0]
        
        let repeatEmoticon = emoticons.filter { (value) -> Bool in
            
            if value.type == emoticon.type{
                
                if value.type == "1"{
                    
                    return value.code == emoticon.code
                    
                }else{
                    
                    return value.chs == emoticon.chs
                }
                
            }else{
                
                return false
            }
        }
        
        for value in repeatEmoticon{
            
            if let index = emoticons.indexOf(value){
                
                emoticons.removeAtIndex(index)
            }
        }
        
        emoticons.insert(emoticon, atIndex: 0)
        
        while emoticons.count > 20{
            
            emoticons.removeLast()
        }
        
        allEmoticons[0][0] = emoticons
        
        NSKeyedArchiver.archiveRootObject(emoticons, toFile: path)
        
    }
    
    private lazy var defaultsEmoticons:[FANEmoticon] = {
       
        return self.emoticonWithPath("default/info.plist")
    }()
    
    private lazy var emojiEmoticons:[FANEmoticon] = {
        
        return self.emoticonWithPath("emoji/info.plist")
    }()
    
    private lazy var lxhEmoticons:[FANEmoticon] = {
        
       return self.emoticonWithPath("lxh/info.plist")
    }()
    
    lazy var allEmoticons:[[[FANEmoticon]]] = {
    
    
        return [
        
            [self.recentEmoticons],
            self.emoticonsPageArray(self.defaultsEmoticons),
            self.emoticonsPageArray(self.emojiEmoticons),
            self.emoticonsPageArray(self.lxhEmoticons),
        ]
    
    }()
    
    func emoticonWithChs(chs:String) -> FANEmoticon?{
        
        for value in defaultsEmoticons{
            
            if value.chs == chs{
                
                return value
            }
        }
        
        for value in lxhEmoticons{
            
            if value.chs == chs{
                
                return value
            }
        }
        
        return nil
        
    }
    
    private func emoticonsPageArray(emoticons:[FANEmoticon]) ->[[FANEmoticon]]{
        
        let pageCount = (emoticons.count - 1) / 20 + 1
        
        var result = [[FANEmoticon]]()
        
        for i in 0..<pageCount {
            
            var length = 20
            
            let location = i*20
            
            if (length+location) > emoticons.count{
                
                length = emoticons.count - location
            }
            
            let range = NSRange(location: location, length: length)
            
            let childArray = (emoticons as NSArray) .subarrayWithRange(range) as! [FANEmoticon]
            
            result.append(childArray)
        }
        
        return result
    }
    
    private func emoticonWithPath(filepath:String) ->[FANEmoticon]{
    
        let path = self.emoticonBundle.pathForResource(filepath, ofType: nil)
        
        let tempArray = NSArray(contentsOfFile: path!)
        
        var mutable = [FANEmoticon]()
        
        for value in tempArray!{
            
            let model = FANEmoticon(dict: value as! [String : AnyObject])
            
            let folderName = (filepath as NSString).stringByDeletingLastPathComponent
            
            model.folderName = folderName
            
            mutable.append(model)
        }
        
        return mutable
        
    }

}
